<?php
/**
 * 订单分拣日志表
 * String id
 * String order_id【订单号】
 * String site
 * String sku
 * String title
 * String size
 * String add_time【分拣时间】
 * String adduser【分拣人】
 * String isgift
 * String statu【0未处理、1已操作未打包、2已打包】
 * String packuser【打包人】
 * String pack_time【打包时间】
 * String operuser【操作人】
 * String oper_time【操作时间】
 */
class App_Model_OrderSortLog extends App_Model_Abstract
{
	public function init()
	{
		parent::init();
		$this->table = App_Db_Table::factory('OrderSortLog');
	}
	
	public function getById($id)
	{
		$row = $this->table->find($id);
		if ($row) {
			return $row->current();
		}
		return array();
	}
	
	public function add($row)
	{
		return $this->table->insert($row);
	}

	public function delete($id)
	{
		$db = $this->table->getAdapter();
		$where = $db->quoteInto('id = ?', $id);
	
		return $this->table->delete($where);
	}
	
	public function update($row, $id)
	{
		$db = $this->table->getAdapter();
		$where = $db->quoteInto('id = ?', $id);
	
		return $this->table->update($row, $where);
	}
	
	/**根据订单号、网站、状态等获取日志*/
	public function getSortLog($fieldArray)
	{
		$select = $this->table->select();
		if($fieldArray!=null){
			foreach ($fieldArray as $field => $value){
				$select->where("$field = ?",$value);
			}
		}

		$select->order('statu ASC');//降序就DESC
		return $this->table->fetchAll($select);
	}
	
	public function listAll($orderid,$sku,$statu,$site,$starttime,$endtime,$page=1,$limit=20)
	{
		$select = $this->table->select();
		if(!empty($orderid)){
			$select->where('order_id = ?',$orderid);
		}
		if(!empty($sku)){
			$select->where('sku = ?',$sku);
		}
		if(!($statu=='')){
			$select->where('statu = ?',$statu);
		}
		if(!empty($site)){
			$select->where('site = ?',$site);
		}
		if(!empty($starttime)){
			$select->where('add_time > ?',$starttime);
		}
		if(!empty($endtime)){
			$select->where('add_time < ?',$endtime);
		}
		$offset = ($page - 1) * $limit;
		$select->limit($limit,$offset);
	
		$select->order('add_time desc');
		return $this->table->fetchAll($select);
	}
	
	public function getTotalNum($orderid,$sku,$statu,$site,$starttime,$endtime)
	{
		$where="where 1 ";
	
		if(!empty($orderid)){
			$where.=" and order_id='$orderid'";
		}
		if(!empty($sku)){
			$where.=" and sku='$sku'";
		}
		if(!empty($statu)){
			$where.=" and statu='$statu'";
		}
		if(!empty($site)){
			$where.=" and site='$site'";
		}
		if(!empty($starttime)){
			$where.=" and add_time>='".$starttime."'";
		}
		if(!empty($endtime)){
			$where.=" and add_time<='".$endtime."'";
		}
		return $this->table->getAdapter()->fetchOne("select count(*) from order_sort_log ".$where);
	}
	
}